//
//  UIControl + Extension.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 12/28/15.
//  Copyright © 2015 RendezvousAuParadis. All rights reserved.
//

import UIKit



extension UIButton {
    /**
    文字颜色默认橙色
    */
    convenience init(title: String?, fontSize: CGFloat, imageName: String?, backgroundImageName: String?, target: AnyObject, action: Selector, textColor: UIColor = UIColor.orangeColor()) {
        self.init()
        
        // 设置字号和颜色，标题
        setTitle(title, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        setTitleColor(textColor, forState: .Normal)
        // 设置图片
        if let opImageName = imageName {
            setImage(UIImage(named: opImageName), forState: .Normal)
            let hilightedImage = UIImage(named: "\(opImageName)_highlighted")
            setImage(hilightedImage, forState: UIControlState.Highlighted)
        }
        
         // 如果可能，设置背景图片
        if let opBackgroundImageName = backgroundImageName {
            let img = UIImage(named: opBackgroundImageName)
            setBackgroundImage(img?.slicedImage(), forState: .Normal)
            
            if let opBackgroundHighlightedImage = UIImage(named: "\(opBackgroundImageName)_highlighted") {
                setBackgroundImage(opBackgroundHighlightedImage.slicedImage(), forState: UIControlState.Highlighted)
            }
        }
        
        // 设置动作方法
        addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)

        // 设置一个初步尺寸
        sizeToFit()
    }
    
    ///  替换按钮的image
    ///
    ///  - parameter imageName:            普通状态image
    ///  - parameter highlightedImageName: 高亮状态image
    func switchImage(imageName: String) {
        
        self.setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
        if let opHighlightedImage = UIImage(named: "\(imageName)_highlighted") {
            self.setImage(opHighlightedImage, forState: UIControlState.Highlighted)
        }
        
    }
}


extension UILabel {
    /**
    默认文字颜色深灰色
    */
    convenience init(text: String?, fontSize: CGFloat, textColor: UIColor = UIColor.darkGrayColor()) {
        self.init()
        // 字号
        font = UIFont.systemFontOfSize(fontSize)
        // 内容
        self.text = text
        self.textColor = textColor
    }
}
